package demo.thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author taqo
 * @date 2021/5/15
 */
public class _线程池有界队列 {
    public static void main(String[] args) {
        RejectedExecutionHandler defaultHandler = (r, executor) -> {
            System.out.println("executor.getCorePoolSize():"+executor.getCorePoolSize());//2
            System.out.println("executor.getMaximumPoolSize():"+executor.getMaximumPoolSize());//5
            System.out.println("executor.getPoolSize():"+executor.getPoolSize());//2
            BlockingQueue<Runnable> queue = executor.getQueue();
            System.out.println("queue.size():"+queue.size());//0??
            executor.shutdown();
            throw new RuntimeException();
        };
        ArrayBlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(5);
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(2, 5, 0L, TimeUnit.MILLISECONDS,
            blockingQueue,defaultHandler);
            while (true) {
                poolExecutor.execute(() -> {
                    System.out.println(Thread.currentThread().getName()+",线程池中队列中的线程数量：" + blockingQueue.size());
                });
            }
    }
}
